package com.fox.stock.mq;

import com.fox.stock.mapper.StockMarketIndexInfoMapper;
import com.fox.stock.service.StockService;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class MqListener {

    @Autowired
    private Cache<String,Object> caffeineCache;

    @Autowired
    private StockService stockService;

    /**
     * 大盘接收信息采集端发送的消息来更新数据缓存
     * @param date
     */
    @RabbitListener(queues = "innerMarketQueue")
    public void refreshInnerMarketInfo(Date date){
        long Time = DateTime.now().getMillis() - new DateTime(date).getMillis();
        if (Time > 60000){
            log.info("采集国内大盘时间:{},同步超时:{}ms",DateTime.now(),Time);
        }

        // 刷新缓存
        // 将缓存置为失效缓存
        caffeineCache.invalidate("innerMarketQueue");
        // 调用服务更新缓存
        stockService.getInnerMarketInfo();
    }

    /**
     * 板块接收信息采集端发送的消息来更新数据缓存
     * @param date
     */
    @RabbitListener(queues = "blockRtInfoQueue")
    public void refreshBlockRtInfo(Date date){
        long Time = DateTime.now().getMillis() - new DateTime(date).getMillis();
        if (Time > 60000){
            log.info("采集板块时间:{},同步超时:{}ms",DateTime.now(),Time);
        }

        // 刷新缓存
        // 将缓存置为失效缓存
        caffeineCache.invalidate("blockRtInfoQueue");
        // 调用服务更新缓存
        stockService.getInnerSectorIndexInfo();
    }


}
